<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Validated,Valid," />










<meta name="description" content="解决平时开发时遇到的联合动态校验问题">
<meta name="keywords" content="Validated,Valid">
<meta property="og:type" content="article">
<meta property="og:title" content="Validated的多字段联合逻辑校验问题">
<meta property="og:url" content="https://scorpiozhoulei.gitee.io/2020/09/06/Validated的多字段联合逻辑校验问题/index.html">
<meta property="og:site_name" content="zlTuring的博客">
<meta property="og:description" content="解决平时开发时遇到的联合动态校验问题">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2020-09-06T14:40:34.983Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Validated的多字段联合逻辑校验问题">
<meta name="twitter:description" content="解决平时开发时遇到的联合动态校验问题">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"right","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://scorpiozhoulei.gitee.io/2020/09/06/Validated的多字段联合逻辑校验问题/"/>





  <title>Validated的多字段联合逻辑校验问题 | zlTuring的博客</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-right page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">zlTuring的博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">zlblog</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://scorpiozhoulei.gitee.io/2020/09/06/Validated的多字段联合逻辑校验问题/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="zlTuring">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/imgs/Menhera-help.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="zlTuring的博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Validated的多字段联合逻辑校验问题</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2020-09-06T21:29:50+08:00">
                2020-09-06
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/问题总结/" itemprop="url" rel="index">
                    <span itemprop="name">问题总结</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          
              <div class="post-description">
                  解决平时开发时遇到的联合动态校验问题
              </div>
          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="问题"><a href="#问题" class="headerlink" title="问题"></a>问题</h1><p>开发时遇到一个这样的校验需求，有这样一个数据结构</p>
<pre><code>{
  &quot;fileName&quot;: &quot;aaaaaa&quot;,
  &quot;fileUploadPtah&quot;: &quot;bbbb&quot;,
  &quot;pcbSizeField&quot;: {
     &quot;boardType&quot;: &quot;Single&quot;,
     &quot;quantity&quot;: &quot;1000&quot;,
     &quot;panelSize&quot;: {
       &quot;sizeX&quot;: &quot;111&quot;,
       &quot;sizeY&quot;: null
     },
     &quot;singleSize&quot;: {
       &quot;sizeX&quot;: &quot;sss&quot;,
       &quot;sizeY&quot;: null
     }
   }
}
</code></pre><p>其中当boardType<br>为Single时，panelSize的sizeX和sizeY可以为空值，singleSize不能为空。<br>为Panel时，panelSize和singleSize的sizeX和sizeY都不能为空。</p>
<p>查看各种资料和blog后，发现如下解决方法</p>
<h1 id="使用-AssertTrue注解"><a href="#使用-AssertTrue注解" class="headerlink" title="使用@AssertTrue注解"></a>使用@AssertTrue注解</h1><p>多个字段联合，可在Bean类中定义方法，结合@AssertTrue注解</p>
<pre><code>@AssertTrue(message = &quot;singleSize的x和y不能为空&quot;)
public boolean isSingleValid(){
    if (this.boardType.equals(&quot;Single&quot;)){
        return !StringUtils.isEmpty(this.singleSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.singleSize.getSizeY());
    }
    return true;
}

@AssertTrue(message = &quot;panelSize的x和y不能为空&quot;)
public boolean isPanelValid(){
    if (this.boardType.equals(&quot;Panel&quot;)){
        return !StringUtils.isEmpty(this.singleSize.getSizeY()) &amp;&amp; !StringUtils.isEmpty(this.singleSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.panelSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.panelSize.getSizeY());
    }
    return true;
}
</code></pre><h1 id="使用非标准的-GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组"><a href="#使用非标准的-GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组" class="headerlink" title="使用非标准的@GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组"></a>使用非标准的@GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组</h1><ol>
<li>实现DefaultGroupSequenceProvider接口(处理器)</li>
</ol>
<pre><code>@Slf4j
public class PcbSizeFieldGroupSequenceProvider implements DefaultGroupSequenceProvider&lt;PcbSizeField&gt; {

     @Override
    public List&lt;Class&lt;?&gt;&gt; getValidationGroups(PcbSizeField bean) {
        List&lt;Class&lt;?&gt;&gt; defaultGroupSequence = new ArrayList&lt;&gt;();
        defaultGroupSequence.add(PcbSizeField.class);
        if (bean != null){
            String boardType = bean.getBoardType();
            log.info(&quot;板类型为--&gt;[{}]&quot;, boardType);
            if (boardType.equals(&quot;Single&quot;)){
                defaultGroupSequence.add(SingleValidationGroup.class);
            }else if(boardType.equals(&quot;Panel&quot;)){
                defaultGroupSequence.add(PanelValidationGroup.class);
            }
        }
        return defaultGroupSequence;
    }
}
</code></pre><ol start="2">
<li>在待校验javaBean里使用@GroupSquenceProvider注解指定处理器</li>
</ol>
<pre><code>@Data
@GroupSequenceProvider(PcbSizeFieldGroupSequenceProvider.class)
public class PcbSizeField implements Serializable {
    private static final long serialVersionUID = -2125038869145258L;
    @NotNull(message = &quot;boardType不能为null&quot;)
    private String boardType;
    @NotNull(message = &quot;数量不能为null&quot;)
    private String quantity;

    @NotBlank(message = &quot;sizeX不能为null&quot;,groups = {SingleValidationGroup.class, PanelValidationGroup.class})
    private String singleSizeX;
    @NotBlank(message = &quot;sizeY不能为null&quot;,groups = {SingleValidationGroup.class, PanelValidationGroup.class})
    private String singleSizeY;
    @NotBlank(message = &quot;panelX不能为null&quot;,groups = PanelValidationGroup.class)
    private String panelSizeX;
    @NotBlank(message = &quot;panelY不能为null&quot;,groups = PanelValidationGroup.class)
    private String panelSizeY;

    private QuoteSize panelSize;
    private QuoteSize singleSize;

    @AssertTrue(message = &quot;singleSize的x和y不能为空&quot;)
    public boolean isSingleValid(){
        if (this.boardType.equals(&quot;Single&quot;)){
            return !StringUtils.isEmpty(this.singleSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.singleSize.getSizeY());
        }
        return true;
    }

    @AssertTrue(message = &quot;panelSize的x和y不能为空&quot;)
    public boolean isPanelValid(){
        if (this.boardType.equals(&quot;Panel&quot;)){
            return !StringUtils.isEmpty(this.singleSize.getSizeY()) &amp;&amp; !StringUtils.isEmpty(this.singleSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.panelSize.getSizeX()) &amp;&amp; !StringUtils.isEmpty(this.panelSize.getSizeY());
        }
        return true;
    }

}
</code></pre><h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>以上两种都能实现效果。对于我现在的需求而言，更适合第一种分法。<br>第二种能适应更多的业务场景。如根据某个字段动态加载不同的检验规则。但其局限性是不太适合如上的数据结构。只能把QuoteSize里的属性都提到该处理类中。</p>
<p>如有更方便的方法，欢迎指出。</p>
<h1 id="参考"><a href="#参考" class="headerlink" title="参考"></a>参考</h1><p><a href="https://juejin.im/post/6844903920699965447" target="_blank" rel="noopener">解决多字段联合逻辑校验问题【享学Spring MVC】</a><br><a href="https://www.zhangbj.com/p/583.html" target="_blank" rel="noopener">Spring Boot使用@Valid校验前端传递的参数</a></p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/Validated/" rel="tag"># Validated</a>
          
            <a href="/tags/Valid/" rel="tag"># Valid</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2020/04/19/React-学习笔记/" rel="next" title="React 学习笔记">
                <i class="fa fa-chevron-left"></i> React 学习笔记
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/imgs/Menhera-help.jpg"
                alt="zlTuring" />
            
              <p class="site-author-name" itemprop="name">zlTuring</p>
              <p class="site-description motion-element" itemprop="description">zlTuring的博客</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">13</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">19</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#问题"><span class="nav-number">1.</span> <span class="nav-text">问题</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用-AssertTrue注解"><span class="nav-number">2.</span> <span class="nav-text">使用@AssertTrue注解</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#使用非标准的-GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组"><span class="nav-number">3.</span> <span class="nav-text">使用非标准的@GroupSequenceProvider注解，动态来决定哪些校验组进入默认校验组</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">4.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考"><span class="nav-number">5.</span> <span class="nav-text">参考</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2020</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">zlTuring</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
